package com.blog.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.blog.common.CommonRespDto;
import com.blog.common.RespBean;
import com.blog.feign.SecAccountFeignClient;
import com.blog.feign.SecGoodsFeignClient;
import com.blog.mapper.SecOrdersMapper;
import com.blog.seckill.goods.Good;
import com.blog.seckill.orders.SecOrders;
import com.blog.seckill.orders.ViewOrder;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SecOrdersBaseService
 * @Destription  SecOrders基础：增删改查
 * @Author 天堂小野花
 * @Date 2025/8/19  18:35
 * @Version 1.0
 */
@Service
public class SecOrdersBaseService {
    @Autowired
    SecOrdersMapper secOrdersMapper;
    @Resource
    SecGoodsFeignClient secGoodsFeignClient;
    @Resource
    SecAccountFeignClient secAccountFeignClient;
    //表 SecOrders
    public SecOrders selectSecOrdersById(Integer id){
        return secOrdersMapper.selectByPrimaryKey(id);
    }
    public int insertSecOrders(SecOrders secOrders){
        return secOrdersMapper.insert(secOrders);
    }

    public int updateSecOrders(SecOrders secOrders){
        return secOrdersMapper.updateByPrimaryKeySelective(secOrders);
    }
    public int deleteSecOrdersById(Integer id){
        return secOrdersMapper.deleteByPrimaryKey(id);
    }

    @SentinelResource(value = "selectAllOrders",fallback = "selectAllOrdersFallBack")
    public List<ViewOrder> selectAllOrders() {
        return secOrdersMapper.selectAllOrders();
    }
    //兜底回调
    public List<ViewOrder> selectAllOrdersFallBack(Throwable e) {
        ArrayList<ViewOrder> viewOrders = new ArrayList<>();
        ViewOrder viewOrder = new ViewOrder();
        viewOrder.setName("异常信息："+e.getClass());
        viewOrders.add(viewOrder);
        return viewOrders;
    }
    @Transactional
    @SentinelResource(value = "createOrder",fallback = "createOrderFallBack")
    public int createOrder(Integer userId,Integer goodId, Integer goodNum) {
        SecOrders secOrders = new SecOrders();
        secOrders.setGoodId(goodId);
        secOrders.setGoodNum(goodNum);
        secOrders.setStatus(0);
        secOrders.setUserId(userId);
        //更新库存
        int goodResult = secGoodsFeignClient.goodInventoryNumById(goodId, goodNum);
        System.out.println(goodResult);
        //查询商品价格
        Good good= secGoodsFeignClient.selectGoodById(goodId);
        //判断是否是兜底回调产生 的数据
        if(good.getProductSpuName().equals("未知商品")){
            secOrders.setStatus(4);
        }
        BigDecimal goodPrice = good.getProductSkuPrice();

        //订单总金额
        BigDecimal goodSum = goodPrice.multiply(BigDecimal.valueOf(goodNum));
        secOrders.setMoneySum(goodSum);
        //账户金额
        int accountResult = secAccountFeignClient.hrAccountAdjustByHrId(goodSum,secOrders.getUserId());
        //判断是否是兜底回调产生 的数据
        if(accountResult == 0){
            secOrders.setStatus(4);
        }
        return secOrdersMapper.insert(secOrders);
    }
    //"创建订单"兜底回调
    public int createOrderFallBack(Integer userId,Integer goodId, Integer goodNum,Throwable throwable) {
        SecOrders secOrders = new SecOrders();
        secOrders.setGoodId(null);
        secOrders.setGoodNum(null);
        secOrders.setStatus(4);
        secOrders.setUserId(userId);
        secOrders.setMoneySum(null);
        return secOrdersMapper.insert(secOrders);
    }
}
